package com.exam.api.common.dto;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * @Author bcheng
 * @Create 2024/1/13 12:09
 * @Description 分页参数类
 */
@EqualsAndHashCode(callSuper = true)
@Schema(description = "分页参数类")
@Data
public class PageDTO extends BaseScopeDTO {

    /**
     * 第几页
     */
    @Schema(description = "第几页")
    @NotNull
    @Min(1)
    private Integer pageNum;

    /**
     * 每页数量
     */
    @Schema(description = "每页数量")
    @NotNull(message = "每页数量")
    private Integer pageSize;

    /**
     * 当前记录起始索引 默认值
     */
    @Schema(description = "当前记录起始索引 默认值")
    public static final int DEFAULT_PAGE_NUM = 1;

    /**
     * 每页显示记录数 默认值 默认查全部
     */
    @Schema(description = "每页显示记录数 默认值 默认查全部")
    public static final int DEFAULT_PAGE_SIZE = Integer.MAX_VALUE;

    /**
     * 构建分页对象
     */
    public <T> IPage<T> buildPage() {
        Integer pageNum = ObjectUtil.defaultIfNull(getPageNum(), DEFAULT_PAGE_NUM);
        Integer pageSize = ObjectUtil.defaultIfNull(getPageSize(), DEFAULT_PAGE_SIZE);
        if (pageNum <= 0) {
            pageNum = DEFAULT_PAGE_NUM;
        }
        Page<T> page = new Page<>(pageNum, pageSize);
       /* List<OrderItem> orderItems = buildOrderItem();
        if (CollUtil.isNotEmpty(orderItems)) {
            page.addOrder(orderItems);
        }*/
        return page;
    }
}
